
import 'package:flutter/material.dart';
import 'package:reabampos/src/reabam/operation/rb_api.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/operation/rb_enum.dart';
import 'package:reabampos/src/reabam/ui/pagestate_scan_qr_code.dart';
import 'package:reabampos/src/reabam/ui/setting/pagestate_select_device_pinpai.dart';
import 'package:reabampos/src/reabam/utils/rb_color.dart';
import 'package:reabampos/src/reabam/utils/rb_widget_ss.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/*
编辑打印机
快麦云打印设备SN: KM118MW23230050
 */
class EditDeviceStatePage extends XPageState{

  String tag;
  XBaseDevice? device;
  bool isEditMode=false;//是否编辑模式,否则新增

  EditDeviceStatePage(
      {
        required this.tag,
        XBaseDevice? currentDevice,
      }
      )
  {
    if(currentDevice!=null){
      String json=XJsonUtils.obj2Json(currentDevice);
      device=XBaseDevice.fromJson(XJsonUtils.json2Map(json));
    }
  }

  List<XBaseDeviceScene> scenes=[];

  String currentSerialNumber='1';

  String currentPaperSize='80';

  String? pinpai;
  String? ip;
  int port=9100;
  //云打印设备sn
  String? cloudDeviceSN;
  //云打印设备密钥 - 如果设备机身贴有密钥，该参数就是必传
  String? cloudDeviceKey;
  bool isSocketShortConnect=true;

  @override
  void onStart() {

    if(device==null){
      //新增打印机
      isEditMode=false;

      if (RBConfigs.IPPrinter == tag) {
        //网络打印机
        pinpai='佳博';
        port=9100;
        device=XBaseDevice(customType:tag,brand: pinpai,port: port,isSocketShortConnect:isSocketShortConnect);
      }else if (RBConfigs.CloudPrinter == tag) {
        //云打印机
        pinpai='快麦';
        device=XBaseDevice(customType:tag,brand: pinpai);
      }else{
        device=XBaseDevice(customType:tag);
      }

      if(
      RBConfigs.versionType==VersionType.officialSelfServiceVersion
          || RBConfigs.versionType==VersionType.testSelfServiceVersion
      ){
        //自助收银
        setXTopBarWidget(
            height: null,
            child: RBWidgets_SelfService.getTitleBarWidget(
              this,
              title: '添加打印机',
            )
        );

      }else{
        //HD h5
        setXTitle(titleText: '添加打印机',
          titleBarHeight: 50,
          isShowLeftWidget: false,
        );

        setXLeftFixSpace(
          width:458,
        );
      }

    }else{
      isEditMode=true;

      ip=device!.ip;
      port=device!.port;
      cloudDeviceSN=device!.deviceUUID;
      cloudDeviceKey=device!.deviceKey;
      pinpai=device!.brand;
      isSocketShortConnect=device!.isSocketShortConnect;

      //编辑网络打印机
      if(RBConfigs.versionType==VersionType.officialSelfServiceVersion
          || RBConfigs.versionType==VersionType.testSelfServiceVersion
      ){
        //自助收银
        setXTopBarWidget(
            height: null,
            child: RBWidgets_SelfService.getTitleBarWidget(
              this,
              title: '编辑打印机',
              rightBt: '移除设备',
              onTapRightBt: (){
                if (RBConfigs.IPPrinter == tag) {
                  //网络打印机
                  XSocketUtils.removeDevice(device!);
                }else if (RBConfigs.BluetoothPrinter == tag) {
                  //蓝牙打印机
                  XBluetoothBLEUtils.removeDevice(device!.bluetoothAddress!);
                }else if (RBConfigs.USBPrinter == tag) {
                  //USB打印机
                  XUsbUtils.removeNoteUsbDevice(device!);
                }else if (RBConfigs.CloudPrinter == tag) {
                  //快麦打印机
                  XRemoteDeviceUtils.removeDevice(device!);
                }

                finishPage(result: "success");
              }
            )
        );

      }else{
        //HD h5
        setXTitle(titleText: '编辑打印机',
          titleBarHeight: 50,
          isShowLeftWidget: false,
          rightWidgets: [
            XWidgetUtils.getWidgetButtonText(
                text: '移除设备',
                buttonStyle: XStyleUtils.getButtonStyle(
                    fontSize: 15, fontColor: RBColors.reabamColor4),
                onPressed: () {
                  if (RBConfigs.IPPrinter == tag) {
                    //网络打印机
                    XSocketUtils.removeDevice(device!);
                  }else if (RBConfigs.BluetoothPrinter == tag) {
                    //蓝牙打印机
                    XBluetoothBLEUtils.removeDevice(device!.bluetoothAddress!);
                  }else if (RBConfigs.USBPrinter == tag) {
                    //USB打印机
                    XUsbUtils.removeNoteUsbDevice(device!);
                  }else if (RBConfigs.CloudPrinter == tag) {
                    //快麦打印机
                    XRemoteDeviceUtils.removeDevice(device!);
                  }

                  finishPage(result: "success");
                }
            )
          ],
        );

        setXLeftFixSpace(
          width:458,
        );
      }
    }


    _initPrintScene();
    currentSerialNumber=device!.serialNumber;
    currentPaperSize=device!.printerPagerSize;

    setXBottomBarWidget(
      child:XWidgetUtils.getLayoutContainer(
        child: XWidgetUtils.getXButtonRoundedRectAngle(
          text: '确认',
          fontColor: Colors.white,
          onPressed: () {
            //设备序号
            device!.serialNumber=currentSerialNumber;
            //纸张尺寸
            device!.printerPagerSize=currentPaperSize;

            //打印场景
            List<XBaseDeviceScene> selectScenes=[];
            for(XBaseDeviceScene item in scenes){
              if(item.isUserSelect){
                selectScenes.add(item);
              }
            }
            device!.scenes=selectScenes;

            if(XStringUtils.isEmpty(device!.deviceName)){
              toast('请输入设备名称');
              return;
            }


            //hyl 2023.10.28 优化:同场景+设备序号唯一
            bool canUpdate=_checkDeviceSettingCanUpdate();
            if (!canUpdate){
              toast("请检查已配置的其他打印机,已存在同场景的相同设备序号设置!");
              return;
            }


            if (RBConfigs.IPPrinter == tag) {
              //网络打印
              if(!isEditMode){
                //新增
                if(!XRegExpUtils.isIP(textEditingController_ip!.value.text)){
                  toast('请输入正确IP地址');
                  return;
                }

                device!.ip=textEditingController_ip!.value.text;
                device!.brand=pinpai;
                device!.port=port;
                device!.isSocketShortConnect=isSocketShortConnect;

                XSocketUtils.addDevice(device!);
              }else{
                //更新
                device!.brand=pinpai;
                device!.port=port;
                XSocketUtils.updateDevice(device!);
              }

              finishPage(result: "success");

            }else if (RBConfigs.BluetoothPrinter == tag) {
              //蓝牙打印机(更新)
              XBluetoothBLEUtils.updateDevice(device!);
              finishPage(result: "success");
            }else if(RBConfigs.USBPrinter==tag){
              //usb打印机
              XUsbUtils.updateNoteUsbDevice(device!);
              finishPage(result: "success");
            }else if (RBConfigs.CloudPrinter == tag) {
              //快麦打印机
              if(!isEditMode){
                //新增
                if(XStringUtils.isEmpty(textEditingController_deviceUUID!.value.text)){
                  toast('请输入云设备SN');
                  return;
                }
                device!.deviceUUID=textEditingController_deviceUUID!.value.text;
                device!.deviceKey=textEditingController_deviceKey!.value.text;
                device!.brand=pinpai;

                //绑定设备
                showLoading();
                RBBaseApi.bindKuaimaiCloudDevice(
                    sn: device!.deviceUUID,
                    deviceKey: device!.deviceKey,
                    bindName: device!.deviceName,
                    noteName:'${device!.deviceName} ${device!.brand}',
                    succeed: (response, Map<String, dynamic> responseMap, Map<String, dynamic> headers_response) {
                      hideLoading();
                      XRemoteDeviceUtils.addDevice(device!);
                      //
                      finishPage(result: "success");
                    },
                    failed: (String code, String msg) {
                      hideLoading();
                      toast(msg);
                    }
                );


              }else{
                //更新
                device!.deviceKey=textEditingController_deviceKey!.value.text;
                device!.brand=pinpai;
                XRemoteDeviceUtils.updateDevice(device!);
                //
                finishPage(result: "success");
              }

            }

          },
          backgroundColor: RBColors.reabamColor4,
        )
      ),
      alignment: Alignment.center,
      padding: XWidgetUtils.getEdgeInsets(RBConfigs.bottomBarButtomMarginLeftRight, 0, RBConfigs.bottomBarButtomMarginLeftRight, 0),
      backgroundColor: Colors.white,
    );
  }


  bool _checkDeviceSettingCanUpdate(){

    //
    List<XBaseDeviceScene>? currentScenes=device!.scenes;
    if (currentScenes==null || currentScenes.isEmpty){
      return true;
    }

    //蓝牙打印机
    Set<XBaseDevice> bluetoothBLEDeviceList=XBluetoothBLEUtils.bluetoothBLEDeviceList;
    for(XBaseDevice noteItemDevice in bluetoothBLEDeviceList){
      if(RBConfigs.BluetoothPrinter==noteItemDevice.customType){
        //非自身
        if(noteItemDevice.bluetoothAddress!=device!.bluetoothAddress){
          List<XBaseDeviceScene>? noteItemScenes=noteItemDevice.scenes;
          if(noteItemScenes!=null && noteItemScenes.isNotEmpty){
            //当前场景
            for(XBaseDeviceScene currentScene in currentScenes){
              for(XBaseDeviceScene noteScene in noteItemScenes){
                //相同场景+序号
                if(currentScene==noteScene && noteItemDevice.serialNumber==device!.serialNumber){
                  return false;
                }
              }
            }

          }
        }
      }
    }

    //网络打印机
    Set<XBaseDevice> socketDeviceList=XSocketUtils.socketDeviceList;
    for(XBaseDevice noteItemDevice in socketDeviceList){
      if(RBConfigs.IPPrinter==noteItemDevice.customType){
        //非自身
        if(noteItemDevice.ip!=device!.ip){
          List<XBaseDeviceScene>? noteItemScenes=noteItemDevice.scenes;
          if(noteItemScenes!=null && noteItemScenes.isNotEmpty){
            //当前场景
            for(XBaseDeviceScene currentScene in currentScenes){
              for(XBaseDeviceScene noteScene in noteItemScenes){
                //相同场景+序号
                if(currentScene==noteScene && noteItemDevice.serialNumber==device!.serialNumber){
                  return false;
                }
              }
            }
          }
        }
      }
    }

    //usb打印机
    Set<XBaseDevice> usbDeviceList=XUsbUtils.usbDeviceList;
    for(XBaseDevice noteItemDevice in usbDeviceList){
      if(RBConfigs.USBPrinter==noteItemDevice.customType){
        //非自身
        if(noteItemDevice.deviceUUID!=device!.deviceUUID){
          List<XBaseDeviceScene>? noteItemScenes=noteItemDevice.scenes;
          if(noteItemScenes!=null && noteItemScenes.isNotEmpty){
            //当前场景
            for(XBaseDeviceScene currentScene in currentScenes){
              for(XBaseDeviceScene noteScene in noteItemScenes){
                //相同场景+序号
                if(currentScene==noteScene && noteItemDevice.serialNumber==device!.serialNumber){
                  return false;
                }
              }
            }
          }
        }
      }
    }

    //远程设备(快麦云打印)打印机
    Set<XBaseDevice> cloudDeviceList=XRemoteDeviceUtils.remoteDeviceList;
    for(XBaseDevice noteItemDevice in cloudDeviceList){
      //非自身
      if(noteItemDevice.deviceUUID!=device!.deviceUUID){
        List<XBaseDeviceScene>? noteItemScenes=noteItemDevice.scenes;
        if(noteItemScenes!=null && noteItemScenes.isNotEmpty){
          //当前场景
          for(XBaseDeviceScene currentScene in currentScenes){
            for(XBaseDeviceScene noteScene in noteItemScenes){
              //相同场景+序号
              if(currentScene==noteScene && noteItemDevice.serialNumber==device!.serialNumber){
                return false;
              }
            }
          }
        }
      }
    }

    return true;
  }

  TextEditingController? textEditingController_deviceName;
  TextEditingController? textEditingController_ip;
  TextEditingController? textEditingController_deviceUUID;
  TextEditingController? textEditingController_deviceKey;


  Widget _getDeviceInfoWidget(){

    List<Widget> children=[];

    //设备名称
    Widget deviceNameWidget=XWidgetUtils.getXBarForLeftRightWidget(
        isShowBottomDivider: false,
        leftWidget: XWidgetUtils.getLayoutContainer(
            width: XTempData.defaultLeftKeyWidth,
            child: XWidgetUtils.getWidgetText(
              '设备名称',
              style: XStyleUtils.getTextStyle(
                  fontSize: XTempData.defaultTextFontSize,
                  color: XColorUtils.color666666
              ),
            )
        ),
        rightWidget: XWidgetUtils.getLayoutExpanded(
            XWidgetUtils.getLayoutContainer(
                child: XWidgetUtils.getWidgetTextField(
                    hintText:'请输入设备名称',
                    hintTextStyle: XStyleUtils.getTextStyle(
                        fontSize: XTempData.defaultTextFontSize,
                        color: XColorUtils.color999999
                    ),
                    controller: textEditingController_deviceName,
                    style: XStyleUtils.getTextStyle(
                        fontSize: XTempData.defaultTextFontSize,
                        color: XColorUtils.color333333
                    ),
                    onChanged: (String text){
                      device!.deviceName=text;
                    }
                )
            )

        )
    );
    children.add(deviceNameWidget);


    if (RBConfigs.IPPrinter == tag) {
      //网络打印
      textEditingController_ip=TextEditingController(text: ip);

      //ip地址
      children.add(
          XWidgetUtils.getXBarForLeftRightWidget(
              isShowBottomDivider: false,
              isShowTopDivider: true,
              leftWidget: XWidgetUtils.getLayoutContainer(
                  width: XTempData.defaultLeftKeyWidth,
                  child: XWidgetUtils.getWidgetText(
                    'IP地址',style: XStyleUtils.getTextStyle(
                      fontSize: XTempData.defaultTextFontSize,
                      color: XColorUtils.color666666
                  ),
                  )
              ),
              rightWidget: XWidgetUtils.getLayoutExpanded(
                  XStringUtils.isEmpty(device!.ip)?
                  XWidgetUtils.getWidgetTextField(
                      hintText:'请输入IP地址',
                      hintTextStyle: XStyleUtils.getTextStyle(
                          fontSize: XTempData.defaultTextFontSize,
                          color: XColorUtils.color999999
                      ),
                      controller: textEditingController_ip,
                      style: XStyleUtils.getTextStyle(
                          fontSize: XTempData.defaultTextFontSize,
                          color: XColorUtils.color333333
                      ),
                      onChanged: (String text){
                        ip=text;
                      }
                  ):XWidgetUtils.getWidgetText('${device!.ip}',style: XStyleUtils.getTextStyle(
                      fontSize: XTempData.defaultTextFontSize,
                      color: XColorUtils.color333333
                  ))
              )
          )
      );
      //是否短连接
      children.add(
          XWidgetUtils.getXBarForLeftRightWidget(
            isShowBottomDivider: false,
            isShowTopDivider: true,
            leftWidget: XWidgetUtils.getLayoutContainer(
                width: XTempData.defaultLeftKeyWidth,
                child: XWidgetUtils.getWidgetText(
                  '短连接',style: XStyleUtils.getTextStyle(
                    fontSize: XTempData.defaultTextFontSize,
                    color: XColorUtils.color666666
                ),
                )
            ),
            rightWidget: XWidgetUtils.getLayoutExpanded(
                XWidgetUtils.getLayoutRow(
                    children: [
                      const Spacer(),
                      XWidgetUtils.getWidgetGestureDetector(
                        child: XWidgetUtils.getWidgetSwitch(
                            currentValue: isSocketShortConnect,
                            onChanged: isEditMode?null:(bool value) {
                              setState(() {
                                isSocketShortConnect=value;
                              });
                            }
                        ),
                        onTap: (){
                          toast('编辑打印机不支持修改,请删除重新添加');
                        }
                      )

                    ]
                )
            ),
            onPressed: (){
            },
          )
      );
      //品牌
      children.add(
          XWidgetUtils.getXBarForLeftRightWidget(
            isShowBottomDivider: false,
            isShowTopDivider: true,
            leftWidget: XWidgetUtils.getLayoutContainer(
                width: XTempData.defaultLeftKeyWidth,
                child: XWidgetUtils.getWidgetText(
                  '设备品牌',
                  style: XStyleUtils.getTextStyle(
                    fontSize: XTempData.defaultTextFontSize,
                    color: XColorUtils.color666666
                ),
                )
            ),
            rightWidget: XWidgetUtils.getLayoutExpanded(
                XWidgetUtils.getLayoutRow(
                    children: [
                      XWidgetUtils.getLayoutExpanded(
                          XWidgetUtils.getWidgetText(
                              XStringUtils.isEmpty(pinpai)?'请选择品牌':pinpai??'',
                              style: XStyleUtils.getTextStyle(
                                  fontSize: XTempData.defaultTextFontSize,
                                  color: XStringUtils.isEmpty(pinpai)?XColorUtils.color999999:XColorUtils.color333333
                              ),
                          )
                      ),
                      XImageUtils.getImageWidgetWithAsset(
                          'nav_arrow_right.png',
                          width:RBConfigs.image_nav_arrow_right_width,
                          height: RBConfigs.image_nav_arrow_right_height
                      )
                    ]
                )
            ),
            onPressed: (){
              startPage(
                  SelectDevicePinaiStatePage(tag: '网络打印机'),
                  resultCallback: (dynamic result){
                    if(result!=null){
                      setState(() {
                        pinpai=result;
                        if('佳博'==pinpai){
                          port=9100;
                        }else{
                          port=9100;
                        }
                      });
                    }
                  }
              );
            },
          )
      );
    }else if(RBConfigs.CloudPrinter == tag){
      //云打印
      textEditingController_deviceUUID=TextEditingController(text: cloudDeviceSN);
      textEditingController_deviceKey=TextEditingController(text: cloudDeviceKey);
      children.add(
          XWidgetUtils.getXBarForLeftRightWidget(
              isShowBottomDivider: false,
              isShowTopDivider: true,
              leftWidget: XWidgetUtils.getLayoutContainer(
                  width: XTempData.defaultLeftKeyWidth,
                  child: XWidgetUtils.getWidgetText(
                    '云设备SN',style: XStyleUtils.getTextStyle(
                      fontSize: XTempData.defaultTextFontSize,
                      color: XColorUtils.color666666
                  ),
                  )
              ),
              rightWidget: XWidgetUtils.getLayoutExpanded(
                  XStringUtils.isEmpty(device!.deviceUUID)?
                  XWidgetUtils.getLayoutContainer(
                      child: XWidgetUtils.getLayoutRow(
                          children: [
                            XWidgetUtils.getLayoutExpanded(
                                XWidgetUtils.getWidgetTextField(
                                    hintText:'请输入设备SN',
                                    hintTextStyle: XStyleUtils.getTextStyle(
                                        fontSize: XTempData.defaultTextFontSize,
                                        color: XColorUtils.color999999
                                    ),
                                    controller: textEditingController_deviceUUID,
                                    style: XStyleUtils.getTextStyle(
                                        fontSize: XTempData.defaultTextFontSize,
                                        color: XColorUtils.color333333
                                    ),
                                    onChanged: (String text){
                                      cloudDeviceSN=text;
                                    }
                                )
                            ),
                            XWidgetUtils.getWidgetGestureDetector(
                                child: XImageUtils.getImageWidgetWithAsset(
                                    'saoyisao_er.png',
                                    width:20,
                                    height: 20
                                ),
                                onTap: (){
                                  startPage(
                                      ScanQrCodeStatePage(),
                                      resultCallback: (dynamic result){
                                        if(result!=null){
                                          //扫描返回
                                          XLogUtils.printLog('---扫描返回=$result');
                                          setState(() {
                                            cloudDeviceSN=result;
                                          });

                                        }
                                      }
                                  );
                                }
                            ),
                          ]
                      )
                  ):XWidgetUtils.getWidgetText('${device!.deviceUUID}',style: XStyleUtils.getTextStyle(
                      fontSize: XTempData.defaultTextFontSize,
                      color: XColorUtils.color333333
                  ))
              )
          )
      );

      children.add(
          XWidgetUtils.getXBarForLeftRightWidget(
              isShowBottomDivider: false,
              isShowTopDivider: true,
              leftWidget: XWidgetUtils.getLayoutContainer(
                  width: XTempData.defaultLeftKeyWidth,
                  child: XWidgetUtils.getWidgetText(
                    '云设备密钥',style: XStyleUtils.getTextStyle(
                      fontSize: XTempData.defaultTextFontSize,
                      color: XColorUtils.color666666
                  ),
                  )
              ),
              rightWidget: XWidgetUtils.getLayoutExpanded(
                XWidgetUtils.getWidgetTextField(
                    hintText:'如果设备机身贴有密钥,必须输入秘钥,否则为空',
                    hintTextStyle: XStyleUtils.getTextStyle(
                        fontSize: XTempData.defaultTextFontSize,
                        color: XColorUtils.color999999
                    ),
                    controller: textEditingController_deviceKey,
                    style: XStyleUtils.getTextStyle(
                        fontSize: XTempData.defaultTextFontSize,
                        color: XColorUtils.color333333
                    ),
                    onChanged: (String text){
                      cloudDeviceKey=text;
                    }
                ),
              )
          )
      );

      //品牌
      children.add(
          XWidgetUtils.getXBarForLeftRightWidget(
            isShowBottomDivider: false,
            isShowTopDivider: true,
            leftWidget: XWidgetUtils.getLayoutContainer(
                width: XTempData.defaultLeftKeyWidth,
                child: XWidgetUtils.getWidgetText(
                  '设备品牌',style: XStyleUtils.getTextStyle(
                    fontSize: XTempData.defaultTextFontSize,
                    color: XColorUtils.color666666
                ),
                )
            ),
            rightWidget: XWidgetUtils.getLayoutExpanded(
                XWidgetUtils.getLayoutRow(
                    children: [
                      XWidgetUtils.getLayoutExpanded(
                          XWidgetUtils.getWidgetText(
                              XStringUtils.isEmpty(pinpai)?'请选择品牌':pinpai??'',
                              style: XStyleUtils.getTextStyle(
                                  fontSize: XTempData.defaultTextFontSize,
                                  color: XStringUtils.isEmpty(pinpai)?XColorUtils.color999999:XColorUtils.color333333
                              )
                          )
                      ),
                      XImageUtils.getImageWidgetWithAsset(
                          'nav_arrow_right.png',
                          width:RBConfigs.image_nav_arrow_right_width,
                          height: RBConfigs.image_nav_arrow_right_height
                      )
                    ]
                )
            ),
            onPressed: (){
              startPage(
                  SelectDevicePinaiStatePage(tag: '云打印机'),
                  resultCallback: (dynamic result){
                    if(result!=null){
                      setState(() {
                        pinpai=result;
                      });
                    }
                  }
              );
            },
          )
      );
    }

    return XWidgetUtils.getXLayoutRoundedRect(
        margin: XWidgetUtils.getEdgeInsets(XTempData.defaultPageMarginLeft, 10, XTempData.defaultPageMarginLeft, 0),
        child: XWidgetUtils.getLayoutColumn(
            children: children
        )
    );
  }


  @override
  Widget getBodyWidget(BuildContext context) {

    textEditingController_deviceName=TextEditingController(text:device!.deviceName);

    List<Widget> mainWidgets=[];

    //设备信息
    mainWidgets.add(_getDeviceInfoWidget());

    //设备序号
    mainWidgets.add(
        XWidgetUtils.getXLayoutRoundedRect(
            margin: XWidgetUtils.getEdgeInsets(XTempData.defaultPageMarginLeft, 10, XTempData.defaultPageMarginRight, 0),
            child: XWidgetUtils.getLayoutColumn(
                children: [
                  XWidgetUtils.getWidgetText(
                      '设备序号',style: XStyleUtils.getTextStyle(
                      fontSize: XTempData.defaultTextFontSize,
                      color: XColorUtils.color666666
                  ),
                      padding: XWidgetUtils.getEdgeInsets(XTempData.defaultLeftRightWidgetMarginLeft, XTempData.defaultPageMarginLeft, 0, 0)
                  ),
                  XWidgetUtils.getLayoutContainer(
                      padding: XWidgetUtils.getEdgeInsets(XTempData.defaultLeftRightWidgetMarginLeft, 0, 0, 0),
                    child: XWidgetUtils.getLayoutRow(
                        crossAxisAlignment:CrossAxisAlignment.center,
                        children: _getSerialNumberWidgets())
                  )

                ]
            )
        )
    );

    //纸张尺寸
    // mainWidgets.add(
    //     XWidgetUtils.getXLayoutRoundedRect(
    //         margin: XWidgetUtils.getEdgeInsets(10, 10, 10, 0),
    //         child: XWidgetUtils.getLayoutColumn(
    //             children: [
    //               XWidgetUtils.getWidgetText(
    //                   '纸张尺寸',
    //                   style: XStyleUtils.textStyle_000_14(),
    //                   padding: XWidgetUtils.getEdgeInsets(10, 10, 0, 0)
    //               ),
    //               XWidgetUtils.getLayoutRow(
    //                   crossAxisAlignment:CrossAxisAlignment.center,
    //                   children: _getPaperSizeWidgets()
    //               )
    //             ]
    //         )
    //     )
    // );

    //打印场景
    mainWidgets.add(_getPrintSceneWidget());

    return XWidgetUtils.getLayoutSingleChildScrollView(
        child: XWidgetUtils.getLayoutColumn(children: mainWidgets)
    );
  }



  Widget _getPrintSceneWidget(){
    List<Widget> children=[];

    Widget sceneNameWidget=XWidgetUtils.getWidgetText(
        '打印场景',
        style: XStyleUtils.getTextStyle(
            fontSize: XTempData.defaultTextFontSize,
            color: XColorUtils.color666666
        ),
        padding: XWidgetUtils.getEdgeInsets(XTempData.defaultLeftRightWidgetMarginLeft, XTempData.defaultPageMarginLeft, 0, XTempData.defaultPageMarginLeft)
    );

    children.add(sceneNameWidget);

    children.add(
        XWidgetUtils.getWidgetListView(
            shrinkWrap:true,
            listData: scenes,
            listItemWidgetFunction: (BuildContext context, int index){
              XBaseDeviceScene scene=scenes[index];
              return XWidgetUtils.getXBarForLeftRightDefault(
                  isShowBottomDivider: false,
                  isShowTopDivider: true,
                  leftText: scene.name!,
                  leftTextStyle: XStyleUtils.getTextStyle(
                      fontSize: XTempData.defaultTextFontSize,
                      color: XColorUtils.color333333
                  ),
                  rightText: '',
                  rightWidget: scene.isUserSelect? XImageUtils.getImageWidgetWithAsset(
                      'select_xuanzhong.png',
                      color:RBColors.reabamColor4,
                      width: RBConfigs.selectImageWidth,
                      height: RBConfigs.selectImageWidth,
                  ):
                  XImageUtils.getImageWidgetWithAsset(
                      'select_weixuanzhong.png',
                      width: RBConfigs.selectImageWidth,
                      height: RBConfigs.selectImageWidth,
                  ),
                  onPressed: (){
                    setState(() {

                      if(scene.id!='4'){
                        //esc
                        device!.printCommandType=0;
                        //
                        for(XBaseDeviceScene item in scenes){
                          if(item.id=='4'){
                            item.isUserSelect=false;
                            break;
                          }
                        }
                        scene.isUserSelect=!scene.isUserSelect;
                      }else{
                        //tspl
                        device!.printCommandType=1;
                        //
                        for(XBaseDeviceScene item in scenes){
                          if(item.id!='4'){
                            item.isUserSelect=false;
                          }
                        }
                        scene.isUserSelect=!scene.isUserSelect;
                      }

                    });
                  }
              );
            }
        )
    );


    return XWidgetUtils.getXLayoutRoundedRect(
        margin:  XWidgetUtils.getEdgeInsets(XTempData.defaultPageMarginLeft, 10, XTempData.defaultPageMarginRight, 10),
        child: XWidgetUtils.getLayoutColumn(children: children)
    );
  }


  @override
  Future<void> updateData({int pageIndex=1}) async {
  }

  ////////////////////////////////////////////////////

  List<Widget> _getSerialNumberWidgets(){
    List<Widget> serialNumberWidgets=[];
    serialNumberWidgets.add(
        XWidgetUtils.getWidgetRadio(
          padding: XWidgetUtils.getEdgeInsets(0, 0, 5, 0),
          currentSelectValue: currentSerialNumber,
          labelValue: '1',
          showText: '1号',
          textStyle: XStyleUtils.getTextStyle(
              fontSize: XTempData.defaultTextFontSize,
              color: XColorUtils.color333333
          ),
          onChanged: (value){
            setState(() {
              currentSerialNumber=value!;
            });
          },
        )
    );
    serialNumberWidgets.add(
        XWidgetUtils.getWidgetRadio(
          padding: XWidgetUtils.getEdgeInsets(0, 0, 5, 0),
          currentSelectValue: currentSerialNumber,
          labelValue: '2',
          showText: '2号',
          textStyle: XStyleUtils.getTextStyle(
              fontSize: XTempData.defaultTextFontSize,
              color: XColorUtils.color333333
          ),
          onChanged: (value){
            setState(() {
              currentSerialNumber=value!;
            });
          },
        )
    );
    serialNumberWidgets.add(
        XWidgetUtils.getWidgetRadio(
          padding: XWidgetUtils.getEdgeInsets(0, 0, 5, 0),
          currentSelectValue: currentSerialNumber,
          labelValue: '3',
          showText: '3号',
          textStyle: XStyleUtils.getTextStyle(
              fontSize: XTempData.defaultTextFontSize,
              color: XColorUtils.color333333
          ),
          onChanged: (value){
            setState(() {
              currentSerialNumber=value!;
            });
          },
        )
    );
    serialNumberWidgets.add(
        XWidgetUtils.getWidgetRadio(
          padding: XWidgetUtils.getEdgeInsets(0, 0, 5, 0),
          currentSelectValue: currentSerialNumber,
          labelValue: '4',
          showText: '4号',
          textStyle: XStyleUtils.getTextStyle(
              fontSize: XTempData.defaultTextFontSize,
              color: XColorUtils.color333333
          ),
          onChanged: (value){
            setState(() {
              currentSerialNumber=value!;
            });
          },
        )
    );
    serialNumberWidgets.add(
        XWidgetUtils.getWidgetRadio(
          padding: XWidgetUtils.getEdgeInsets(0, 0, 5, 0),
          currentSelectValue: currentSerialNumber,
          labelValue: '5',
          showText: '5号',
          textStyle: XStyleUtils.getTextStyle(
              fontSize: XTempData.defaultTextFontSize,
              color: XColorUtils.color333333
          ),
          onChanged: (value){
            setState(() {
              currentSerialNumber=value!;
            });
          },
        )
    );
    serialNumberWidgets.add(
        XWidgetUtils.getWidgetRadio(
          padding: XWidgetUtils.getEdgeInsets(0, 0, 5, 0),
          currentSelectValue: currentSerialNumber,
          labelValue: '6',
          showText: '6号',
          textStyle: XStyleUtils.getTextStyle(
              fontSize: XTempData.defaultTextFontSize,
              color: XColorUtils.color333333
          ),
          onChanged: (value){
            setState(() {
              currentSerialNumber=value!;
            });
          },
        )
    );
    serialNumberWidgets.add(
        XWidgetUtils.getWidgetRadio(
          padding: XWidgetUtils.getEdgeInsets(0, 0, 5, 0),
          currentSelectValue: currentSerialNumber,
          labelValue: '7',
          showText: '7号',
          textStyle: XStyleUtils.getTextStyle(
              fontSize: XTempData.defaultTextFontSize,
              color: XColorUtils.color333333
          ),
          onChanged: (value){
            setState(() {
              currentSerialNumber=value!;
            });
          },
        )
    );

    return serialNumberWidgets;
  }
  // List<Widget> _getPaperSizeWidgets(){
  //   List<Widget> paperSizeWidgets=[];
  //   paperSizeWidgets.add(
  //       XWidgetUtils.getWidgetRadio(
  //         padding: XWidgetUtils.getEdgeInsets(0, 0, 10, 0),
  //         currentSelectValue: currentPaperSize,
  //         labelValue: '58',
  //         showText: '58mm',
  //         textStyle: XStyleUtils.textStyle_333_14(),
  //         onChanged: (value){
  //           setState(() {
  //             currentPaperSize=value!;
  //           });
  //         },
  //       )
  //   );
  //   paperSizeWidgets.add(
  //       XWidgetUtils.getWidgetRadio(
  //         padding: XWidgetUtils.getEdgeInsets(0, 0, 10, 0),
  //         currentSelectValue: currentPaperSize,
  //         labelValue: '80',
  //         showText: '80mm',
  //         textStyle: XStyleUtils.textStyle_333_14(),
  //         onChanged: (value){
  //           setState(() {
  //             currentPaperSize=value!;
  //           });
  //         },
  //       )
  //   );
  //
  //   return paperSizeWidgets;
  // }

  void _initPrintScene(){
    scenes.clear();
    scenes.add(XBaseDeviceScene(id: '1',name: '外卖订单打印'));
    scenes.add(XBaseDeviceScene(id: '2',name: '线下订单打印'));
    scenes.add(XBaseDeviceScene(id: '3',name: '后厨打印'));
    scenes.add(XBaseDeviceScene(id: '4',name: '标签打印'));
    scenes.add(XBaseDeviceScene(id: '5',name: '商城/点餐打印'));
    scenes.add(XBaseDeviceScene(id: '6',name: '外卖后厨打印'));
    scenes.add(XBaseDeviceScene(id: '7',name: '商品调拨打印'));

    //
    List<XBaseDeviceScene>? deviceScenes=device!.scenes;
    if(deviceScenes!=null&&deviceScenes.isNotEmpty){
      for(XBaseDeviceScene item in deviceScenes){
        for(XBaseDeviceScene selectItem in scenes){
          if(item==selectItem){
            selectItem.isUserSelect=true;
            break;
          }
        }
      }
    }

  }


}